// 设备相关API接口
import type { DeviceInfo } from '@/store/device'

// 模拟API请求延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

// 模拟API响应数据
interface ApiResponse<T> {
  code: number
  message: string
  data: T
}

// 获取设备列表
export async function getDeviceList(): Promise<ApiResponse<DeviceInfo[]>> {
  // 模拟网络请求
  await delay(1000)
  
  // 模拟设备数据
  const devices: DeviceInfo[] = [
    {
      id: 'D001',
      name: '1号洗车机',
      location: 'A区01号',
      status: 'available',
      type: 'standard',
      price: 15,
      waitTime: 0
    },
    {
      id: 'D002',
      name: '2号洗车机',
      location: 'A区02号',
      status: 'occupied',
      type: 'standard',
      price: 15,
      waitTime: 5
    },
    {
      id: 'D003',
      name: '3号洗车机',
      location: 'B区01号',
      status: 'available',
      type: 'premium',
      price: 25,
      waitTime: 0
    },
    {
      id: 'D004',
      name: '4号洗车机',
      location: 'B区02号',
      status: 'maintenance',
      type: 'premium',
      price: 25,
      waitTime: 0
    },
    {
      id: 'D005',
      name: '5号洗车机',
      location: 'C区01号',
      status: 'offline',
      type: 'standard',
      price: 15,
      waitTime: 0
    }
  ]
  
  return {
    code: 200,
    message: '获取成功',
    data: devices
  }
}

// 获取设备详情
export async function getDeviceDetail(deviceId: string): Promise<ApiResponse<DeviceInfo | null>> {
  // 模拟网络请求
  await delay(500)
  
  // 模拟获取设备详情
  const devices = (await getDeviceList()).data
  const device = devices.find(d => d.id === deviceId) || null
  
  return {
    code: device ? 200 : 404,
    message: device ? '获取成功' : '设备不存在',
    data: device
  }
}

// 启动设备
export async function startDevice(deviceId: string): Promise<ApiResponse<null>> {
  // 模拟网络请求
  await delay(1000)
  
  // 模拟启动设备
  return {
    code: 200,
    message: '设备启动成功',
    data: null
  }
}

// 停止设备
export async function stopDevice(deviceId: string): Promise<ApiResponse<null>> {
  // 模拟网络请求
  await delay(1000)
  
  // 模拟停止设备
  return {
    code: 200,
    message: '设备停止成功',
    data: null
  }
}

// 获取设备状态
export async function getDeviceStatus(deviceId: string): Promise<ApiResponse<{ status: DeviceInfo['status'] }>> {
  // 模拟网络请求
  await delay(500)
  
  // 模拟获取设备状态
  return {
    code: 200,
    message: '获取成功',
    data: {
      status: 'available'
    }
  }
}